﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO.Ports;
using System.Threading;

namespace WindowsFormsApplication1
{
    class serialPort
    {
        SerialPort communicate;
        StringBuilder outBuffer = new StringBuilder();
        private string p;

        public serialPort()
        {
            communicate = new SerialPort("COM17", 4800, Parity.None, 8, StopBits.One);
            communicate.Open();
            
        }

        public serialPort(string p)
        {
            // TODO: Complete member initialization
            this.p = p;
            communicate = new SerialPort(p, 4800, Parity.None, 8, StopBits.One);
            communicate.Open();
        }

        public void write()
        {
            char[] escapeSeq = new char[2];
            char[] inBuffer = new char[10];


            communicate.WriteLine("Hello World");
            //return;
            //escapeSeq[0] = 'l';
            //communicate.Write(escapeSeq, 0, 1);
            //Thread.Sleep(100);
            //communicate.Write(escapeSeq, 0, 1);
            //Thread.Sleep(100);
            //communicate.Write(escapeSeq, 0, 1);
            //Thread.Sleep(100);
            //communicate.Write(escapeSeq, 0, 1);
            //Thread.Sleep(100);

            try
            {

                outBuffer.Append((char) communicate.ReadByte());
                //outBuffer.Append(communicate.ReadLine());
                //while (communicate.Read(inBuffer, 0, 1) != 0)
                //{
                //    outBuffer.Append(inBuffer);
                //}

            }
            catch (TimeoutException doneRead)
            {
                outBuffer.Append(doneRead.Message);
            }

            //communicate.Close();

        }

        public String getBuffer()
        {
            return outBuffer.ToString();
        }

    }
}
